package com.mall.controller;

import com.mall.dto.ActionResult;
import com.mall.po.Invoice;
import com.mall.service.InvoiceService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.ArrayList;
import java.util.List;

/**
 * @ClassName InvoiceController
 * @Description TODO
 * @action luoliang
 * @Data 2021/1/6 16:12
 * @Version 1.0
 */
@Api(tags = "发货单列表")
@RestController
@RequestMapping("/shangcheng")
public class InvoiceController {

    @Autowired
    private InvoiceService invoiceService;

    /**
     * 增
     * @param invoice
     * @return
     */
    @ApiOperation(value = "添加商品信息")
    @ApiImplicitParam(name = "invoice",value = "添加一个商品）",required = true)
    @RequestMapping(value = "/deliveradd",method = RequestMethod.POST)
    public ActionResult AddInvoice( @RequestBody Invoice invoice){

        int addinsert = invoiceService.addinsert(invoice);

        if (addinsert >0){
            ActionResult actionResult = new ActionResult(200, "success", addinsert);
            return actionResult;
        }else{
            ActionResult actionResult = new ActionResult(500, "error", null);
            return  actionResult;
        }
    }

    /**
     * 删
     * @param deliverid
     * @return
     */
    @ApiOperation(value = "删除商品信息")
    @ApiImplicitParam(name = "deliverid",value = "删除一个商品）",required = true)
    @RequestMapping(value = "/deliverdel",method = RequestMethod.DELETE)
    public ActionResult DelectInvoice(Integer deliverid){

        int i = invoiceService.deleteByid(deliverid);
        if (i>-1){
            ActionResult actionResult2 = new ActionResult(200, "success", null);
            return actionResult2;
        }else{
            ActionResult actionResult2 = new ActionResult(500, "error", null);
            return actionResult2;
        }

    }

    /**
     * 修改
     * @param record
     * @return
     */
    @ApiOperation(value = "修改商品信息")
    @ApiImplicitParam(name = "record",value = "修改一个商品）",required = true)
    @RequestMapping(value = "/deliverupdate",method = RequestMethod.PUT)
    public ActionResult UpdateInvoice(@RequestBody Invoice record){

        int i = invoiceService.updateInvoice(record);

        if (i >-1 ){
            ActionResult actionResult3 = new ActionResult(200, "success", i);
            return actionResult3;
        }else {
            ActionResult actionResult3 = new ActionResult(500, "error", i);
            return actionResult3;
        }
    }

    /**
     * 通过id查询
     * @param deliverid
     * @return
     */
    @ApiOperation(value = "查询一个商品信息")
    @ApiImplicitParam(name = "deliverid",value = "查询一个商品）",required = true)
    @RequestMapping(value = "/deliver",method = RequestMethod.GET)
    public ActionResult SelectInvoice(Integer deliverid){

        Invoice invoice = invoiceService.selectByid(deliverid);

        if (invoice != null){
            ActionResult actionResult4 = new ActionResult(200, "success", invoice);

            return actionResult4;
        }else {
            ActionResult actionResult4 = new ActionResult(500, "success", invoice);
            return actionResult4;
        }
    }

    /**
     * 批量删除
     * @param deliverids
     * @return
     */
    @ApiOperation(value = "批量删除商品信息")
    @ApiImplicitParam(name = "deliverids",value = "批量删除一个商品）",required = true)
    @RequestMapping(value = "/selectids",method = RequestMethod.DELETE)

    public ActionResult DelectInvoiceByids(Integer[] deliverids){

        boolean b = invoiceService.delectByids(deliverids);


        ActionResult success = new ActionResult(200, "success", b);
        return success;
    }

    /**
     * 查询分页
     * @param page
     * @param pagesize
     * @return
     */
    @ApiOperation(value = "分页获取发货商品信息列表")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "page",value = "查询的页码",required = true ),
            @ApiImplicitParam(name = "pagesize",value = "每一页包含数据条数",required = true)
    })
    @RequestMapping(value = "/selectInvoicePage",method = RequestMethod.GET)
    public List<Invoice> SelectInvoicePage(int page,int pagesize){

        List<Invoice> invoices = invoiceService.selectPage(page, pagesize);

        return invoices;
    }
}
